package com.sf.dlb.web.controller;


import com.sf.dlb.bean.*;
import com.sf.dlb.constant.ResponseConstant;
import com.sf.dlb.jpa.model.TokenEntity;
import com.sf.dlb.web.service.SysUserService;
import com.sf.dlb.web.service.UserFeedbackService;
import com.sf.dlb.utils.AES256EncryptionUtils;
import com.sf.dlb.utils.GsonUtils;
import com.sf.dlb.utils.ResultVOUtil;
import com.sf.dlb.vo.ResultVO;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


/**
 * 后台管理用户
 * Created by jing.tan on 2018/12/10.
 */
@RestController
@RequestMapping("/v1/sysUser/")
public class SysUserController extends BaseController{
    private static Logger logger = LoggerFactory.getLogger(SysUserController.class);
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private UserFeedbackService userFeedbackService;

    /**
     * 登录
     */
    @PostMapping(value = "/login")
    @ResponseBody
    public Object login(@RequestBody AESRquestBean reqeustData) {
        if(null==reqeustData||StringUtils.isBlank(reqeustData.getData())){
            return ResultVOUtil.error(ResponseConstant.ERROR, "参数不合法");
        }
        logger.info("login reqeustData === " + reqeustData);
        String decryptStr=AES256EncryptionUtils.getInstance().decrypt(reqeustData.getData());
        if(StringUtils.isBlank(decryptStr)){
            return ResultVOUtil.error(ResponseConstant.ERROR, "参数不合法");
        }
        logger.info("login reqeustData decryptStr === " + decryptStr);
        SysUserRquestBean sysuserRquestBean=null;
        try {
            sysuserRquestBean= (SysUserRquestBean) GsonUtils.getInstance().fromJson(decryptStr, SysUserRquestBean.class);
        }catch (Exception e){
            e.printStackTrace();
            return ResultVOUtil.error(ResponseConstant.ERROR, "参数不合法");
        }
        //判断参数是否合法
        if (null==sysuserRquestBean) {
            return ResultVOUtil.error(ResponseConstant.ERROR, "参数不合法");
        }
        logger.info("login request json === " + GsonUtils.getInstance().toJson(sysuserRquestBean));
        if(StringUtils.isEmpty(sysuserRquestBean.getLoginName())){
            return ResultVOUtil.error(ResponseConstant.ERROR, "用户名不能为空");
        }
        if(StringUtils.isEmpty(sysuserRquestBean.getPassword())){
            return ResultVOUtil.error(ResponseConstant.ERROR, "密码不能为空");
        }
        ResultVO resultVO=sysUserService.loginPasswrod(sysuserRquestBean);
        logger.info("login response json === " + GsonUtils.getInstance().toJson(resultVO));
        ResultVOUtil.successEncryption(resultVO);//加密
        return resultVO;
    }

    /**
     * 用户管理列表
     */
    @PostMapping(value = "/userList")
    @ResponseBody
    public Object userList(HttpServletRequest request, @RequestBody UserListRequestBean reqeustData) {
        ResultVO resultVO=(ResultVO)request.getAttribute("token");
        //判断参数是否合法
        if (null==reqeustData) {
            return ResultVOUtil.error(ResponseConstant.ERROR, "参数不合法");
        }
        logger.info("userList request json === " + GsonUtils.getInstance().toJson(reqeustData));
        if(reqeustData.getCurrentPages()==0){
            reqeustData.setCurrentPages(reqeustData.getCurrentPages()+1);
        }
        if(reqeustData.getPageSize()==0){
            reqeustData.setPageSize(20);//默认20条
        }
        //获取用户userId
        resultVO=sysUserService.getUserList(reqeustData);
        logger.info("userList response json === " + GsonUtils.getInstance().toJson(resultVO));
        return resultVO;
    }

    /**
     * 用户管理每日阅读行为-统计
     */
    @PostMapping(value = "/userReaderList")
    @ResponseBody
    public Object userReaderList(HttpServletRequest request, @RequestBody UserListRequestBean reqeustData) {
        ResultVO resultVO=(ResultVO)request.getAttribute("token");
        //判断参数是否合法
        if (null==reqeustData) {
            return ResultVOUtil.error(ResponseConstant.ERROR, "参数不合法");
        }
        logger.info("userReaderList request json === " + GsonUtils.getInstance().toJson(reqeustData));
        if(StringUtils.isBlank(reqeustData.getUserId())){
            return ResultVOUtil.error(ResponseConstant.ERROR, "用户id不能为空");
        }
        if(reqeustData.getCurrentPages()==0){
            reqeustData.setCurrentPages(1);//mybtis默认 从1开始算
        }
        if(reqeustData.getPageSize()==0){
            reqeustData.setPageSize(20);//默认20条
        }
        //获取用户userId
        resultVO=sysUserService.getUserReaderList(reqeustData);
        logger.info("userReaderList response json === " + GsonUtils.getInstance().toJson(resultVO));
        return resultVO;
    }
    /**
     * 用户管理下级好友收益-统计
     */
    @PostMapping(value = "/userFriendList")
    @ResponseBody
    public Object userFriendList(HttpServletRequest request, @RequestBody UserListRequestBean reqeustData) {
        ResultVO resultVO=(ResultVO)request.getAttribute("token");
        //判断参数是否合法
        if (null==reqeustData) {
            return ResultVOUtil.error(ResponseConstant.ERROR, "参数不合法");
        }
        logger.info("userFriendList request json === " + GsonUtils.getInstance().toJson(reqeustData));
        if(StringUtils.isBlank(reqeustData.getUserId())){
            return ResultVOUtil.error(ResponseConstant.ERROR, "用户id不能为空");
        }
        if(reqeustData.getCurrentPages()==0){
            reqeustData.setCurrentPages(1);//mybtis默认 从1开始算
        }
        if(reqeustData.getPageSize()==0){
            reqeustData.setPageSize(20);//默认20条
        }
        //获取用户userId
        resultVO=sysUserService.getUserFriendList(reqeustData);
        logger.info("userFriendList response json === " + GsonUtils.getInstance().toJson(resultVO));
        return resultVO;
    }
    /**
     * 用户反馈列表
     *
     * @return
     */
    @PostMapping(value = "/userFeedbackList")
    @ResponseBody
    public Object userFeedbackList(HttpServletRequest request,@RequestBody UserFeedbackBean reqeustData) {
        ResultVO resultVO=(ResultVO)request.getAttribute("token");
        if(reqeustData.getCurrentPages()!=0){
            reqeustData.setCurrentPages(reqeustData.getCurrentPages()-1);//从0开始算；
        }
        if(reqeustData.getPageSize()==0){
            reqeustData.setPageSize(20);//默认20条
        }
        //判断参数是否合法
        resultVO=userFeedbackService.userFeedbackList(reqeustData);
        logger.info("userFeedbackList response json === " + GsonUtils.getInstance().toJson(resultVO));
        return resultVO;
    }
    /**
     * 用户反馈状态更新
     *
     * @return
     */
    @PostMapping(value = "/userFeedbackUpdate")
    @ResponseBody
    public Object userFeedbackUpdate(HttpServletRequest request,@RequestBody UserFeedbackBean reqeustData) {
        ResultVO resultVO=(ResultVO)request.getAttribute("token");
        logger.info("userFeedbackUpdate request json === " + GsonUtils.getInstance().toJson(reqeustData));
        //判断参数是否合法
        if(StringUtils.isBlank(reqeustData.getId())){
            return ResultVOUtil.error(ResponseConstant.ERROR,"参数不合法");
        }
        if(StringUtils.isBlank(reqeustData.getState())){
            return ResultVOUtil.error(ResponseConstant.ERROR,"参数不合法");
        }
        resultVO=userFeedbackService.userFeedbackUpdate(reqeustData);
        logger.info("userFeedbackUpdate response json === " + GsonUtils.getInstance().toJson(resultVO));
        return resultVO;
    }

    /**
     * 用户实名认证列表
     */
    @PostMapping(value = "/userRealInfoList")
    @ResponseBody
    public Object userRealInfoList(HttpServletRequest request,@RequestBody UserListRequestBean reqeustData) {
        ResultVO resultVO=(ResultVO)request.getAttribute("token");
        //判断参数是否合法
        if (null==reqeustData) {
            return ResultVOUtil.error(ResponseConstant.ERROR, "参数不合法");
        }
        logger.info("userRealInfoList request json === " + GsonUtils.getInstance().toJson(reqeustData));
        if(reqeustData.getCurrentPages()!=0){
            reqeustData.setCurrentPages(reqeustData.getCurrentPages()-1);//从0开始算；
        }
        if(reqeustData.getPageSize()==0){
            reqeustData.setPageSize(20);//默认20条
        }
        //获取用户userId
        TokenEntity tokenEntity =(TokenEntity)resultVO.getData();
        reqeustData.setUserId(tokenEntity.getUserId());
        resultVO=sysUserService.userRealInfoList(reqeustData);
        logger.info("userRealInfoList response json === " + GsonUtils.getInstance().toJson(resultVO));
        return resultVO;
    }
    /**
     * 更新用户实名认证状态
     */
    @PostMapping(value = "/updateUserRealState")
    @ResponseBody
    public Object updateUserRealState(HttpServletRequest request,@RequestBody UserListRequestBean reqeustData) {
        ResultVO resultVO=(ResultVO)request.getAttribute("token");
        //判断参数是否合法
        if (null==reqeustData) {
            return ResultVOUtil.error(ResponseConstant.ERROR, "参数不合法");
        }
        if (StringUtils.isBlank(reqeustData.getRealState())) {
            return ResultVOUtil.error(ResponseConstant.ERROR, "审核状态不能为空");
        }
        if (StringUtils.isBlank(reqeustData.getUserId())) {
            return ResultVOUtil.error(ResponseConstant.ERROR, "用户Id不能为空");
        }
        logger.info("updateUserRealState reqeust json === " + GsonUtils.getInstance().toJson(reqeustData));
        //获取用户userId
        resultVO=sysUserService.updateUserRealState(reqeustData);
        logger.info("updateUserRealState response json === " + GsonUtils.getInstance().toJson(resultVO));
        return resultVO;
    }
    /**
     * 用户提现列表
     */
    @PostMapping(value = "/userExtractList")
    @ResponseBody
    public Object userExtractList(HttpServletRequest request,@RequestBody UserListRequestBean reqeustData) {
        ResultVO resultVO=(ResultVO)request.getAttribute("token");
        //判断参数是否合法
        if (null==reqeustData) {
            return ResultVOUtil.error(ResponseConstant.ERROR, "参数不合法");
        }
        logger.info("userExtractList request json === " + GsonUtils.getInstance().toJson(reqeustData));
        if(reqeustData.getCurrentPages()!=0){
            reqeustData.setCurrentPages(reqeustData.getCurrentPages()-1);//从0开始算；
        }
        if(reqeustData.getPageSize()==0){
            reqeustData.setPageSize(20);//默认20条
        }
        //获取用户userId
        TokenEntity tokenEntity =(TokenEntity)resultVO.getData();
        reqeustData.setUserId(tokenEntity.getUserId());
        resultVO=sysUserService.extractList(reqeustData.getExtractState(),reqeustData.getCurrentPages(),reqeustData.getPageSize());
        logger.info("userExtractList response json === " + GsonUtils.getInstance().toJson(resultVO));
        return resultVO;
    }
    /**
     * 重新提交提现任务
     */
    @PostMapping(value = "/extractResend")
    @ResponseBody
    public Object extractResend(HttpServletRequest request,@RequestBody UserListRequestBean reqeustData) {
        ResultVO resultVO=(ResultVO)request.getAttribute("token");
        //判断参数是否合法
        if (null==reqeustData) {
            return ResultVOUtil.error(ResponseConstant.ERROR, "参数不合法");
        }
        logger.info("extractResend request json === " + GsonUtils.getInstance().toJson(reqeustData));
        if(StringUtils.isBlank(reqeustData.getExtractNumber())){
            return ResultVOUtil.error(ResponseConstant.ERROR, "提现流水号不能为空");
        }
        //获取用户userId
        TokenEntity tokenEntity =(TokenEntity)resultVO.getData();
        reqeustData.setUserId(tokenEntity.getUserId());
        resultVO=sysUserService.extractResend(reqeustData.getExtractNumber());
        logger.info("extractResend response json === " + GsonUtils.getInstance().toJson(resultVO));
        return resultVO;
    }
}
